# -*- coding: utf-8 -*-
# Copyright (C) 2007 Boris Dušek
# See the file LICENCE in the top-level directory of this distribution
# for licensing information.
# vim:ts=4:sw=4:et

from army.core import memory

class FileMemoryEngine(memory.MemoryEngine):
    """Implements storing memory in a file (a.k.a. swap)"""

    def __init__(self,file):
        """Initialization.

        file - File-like object in which to store memory

        """
        self.file = file

    def read(self,address):
        self.file.seek(address)
        return self.read_many(address,1)

    def write(self,address,byte):
        self.write_many(address,byte)

    def read_many(self,address,count):
        self.file.seek(address)
        return self.file.read(count)

    def write_many(self,address,data):
        self.file.seek(address)
        self.file.write(data)
